/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
class XLine {
    static Make(x1, y1, x2, y2) {
        let a, b, c;
        if (x1 == x2) {
            a = 1;
            b = 0;
            c = -x1;
        }
        else if (y1 == y2) {
            a = 0;
            b = 1;
            c = -y1;
        }
        else {
            a = (y2 - y1) / (x2 - x1);
            b = -1;
            c = y1 - a * x1;
        }
        return [a, b, c];
    }
    static Move(abc, x, y) {
        return [abc[0], abc[1], abc[2] - abc[0] * x - abc[1] * y];
    }
    static GetIntersection(a1, b1, c1, a2, b2, c2) {
        let determinant = a1 * b2 - a2 * b1;
        if (Math.abs(determinant) <0.001) {
            // 如果行列式为0，说明两条直线平行或者重合，返回null
            return null;
        }

        let x = -(b2 * c1 - b1 * c2) / determinant;
        let y = -(a1 * c2 - a2 * c1) / determinant;
        return [x, y];
    }
    constructor(x1, y1, x2, y2) {//ax+by+c=0
        [this.a, this.b, this.c] = XLine.Make(x1, y1, x2, y2);
    }
    calcY(x) {
        if (this.b == 0) {
            return null;
        }
        return (-this.a * x - this.c) / this.b;
    }
    calcX(y) {
        if (this.a == 0) {
            return null;
        }
        return (-this.b * y - this.c) / this.a;
    }
    moveXY(x, y) {
        this.c -= this.a * x;
        this.c -= this.b * y;
    }

    getIntersection(l) {
        return XLine.GetIntersection(this.a, this.b, this.c, l.a, l.b, l.c);
    }
}

module.exports = {
    XLine
}